package com.zmn.plat.services.impl.pilot;

import com.zmn.common.utils.date.DateUtil;
import com.zmn.plat.model.entity.pilot.ChannelPriceTO;
import com.zmn.plat.model.entity.pilot.ServCityPrice;
import com.zmn.plat.model.entity.pilot.ServCityPriceQuery;
import com.zmn.plat.persistence.interfaces.pilot.ServCityPriceDao;
import com.zmn.plat.services.interfaces.pilot.ServCityPriceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import com.zmn.common.constant.GlobalConsts;

/**
 * 描述: 城市试点接口实现类
 *
 * @author guoqiao
 * @since  2021-06-25 10:17:20
 */
@Slf4j
@Service
public class ServCityPriceServiceImpl implements ServCityPriceService {

    private static final String TAG = "城市试点基础服务接口";

    @Resource
    private ServCityPriceDao servCityPriceDao;

    @Override
    public ServCityPrice findById(Integer relationId) {

        if (Objects.isNull(relationId)) {
            log.error("[{}],根据id查询数据relationId为null", TAG);
            return null;
        }
        return servCityPriceDao.findById(relationId);
    }

    @Override
    public List<ServCityPrice> listPageByQuery(ServCityPriceQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return null;
        }
        query.setTotalCount(servCityPriceDao.countByQuery(query));
        return servCityPriceDao.listPageByQuery(query);
    }

    @Override
    public List<ServCityPrice> listByQuery(ServCityPriceQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return null;
        }
        return servCityPriceDao.listByQuery(query);
    }

    @Override
    @Cacheable(cacheNames = "redis5m", key = "'pilot:city:listPilotTOByQuery:ChannelPriceTO:'+#p0", unless = "#result == null")
    public List<ChannelPriceTO> listPriceTOByQuery(ChannelPriceTO query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return null;
        }
        return servCityPriceDao.listPriceTOByQuery(query);
    }

    @Override
    public Integer countByQuery(ServCityPriceQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件统计数据,query为null", TAG);
            return null;
        }
        return servCityPriceDao.countByQuery(query);
    }

    @Override
    public Integer updateByKey(ServCityPrice servCityPrice) {

        if (Objects.isNull(servCityPrice.getRelationId())){
            log.error("[{}],根据id修改数据,servCityPrice为null", TAG);
            return null;
        }
        return servCityPriceDao.updateByKey(servCityPrice);
    }

    @Override
    public void updateStatusBatch(List<Integer> ids, Integer status, String operator) {

        if (!Objects.equals(status, GlobalConsts.NO) && !Objects.equals(status, GlobalConsts.YES)) {
            log.error("[{}],status状态超出取值范围", TAG);
            return;
        }
        servCityPriceDao.updateStatusBatch(ids, status, operator, DateUtil.getNow());
    }

    @Override
    public Integer insert (ServCityPrice servCityPrice){

        if (Objects.isNull(servCityPrice)){
            log.error("[{}],新增数据,servCityPrice为null", TAG);
            return null;
        }
        return servCityPriceDao.insert(servCityPrice);
    }

    @Override
    public Integer deleteByKey(Integer relationId) {

        if (Objects.isNull(relationId)){
            log.error("[{}],根据id删除数据,relationId为null", TAG);
            return null;
        }
        return servCityPriceDao.deleteByKey(relationId);
    }

}